<?php
if(!defined('IN_ORA')) {
	exit('Access Denied');
}
$GLOBALS['__page_cache__'] = true;
$act = $GLOBALS['act'] ==""?"index":$GLOBALS['act'];
$GLOBALS['tpl']->assign('act',$act);

/*** 查看产品 ***/
if($act == "v"){
	$id = intval($GLOBALS['_param']['id']);
	if(!$GLOBALS['tpl']->isCached("pro-v-id-$id")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 产品信息 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
		$GLOBALS['tpl']->assign('cates',$cates);

		$sql  = "select op.*,oc.cat_name,oc.path,ocnt.content,opa.attr from ".tn('product')." op left join ".tn('cat')." oc ";
		$sql .=" on oc.Id = op.cat_id left join ".tn('content')." ocnt on ocnt.fk_id = op.Id ";
		$sql .=" left join ".tn('pattr')." opa on opa.p_id = op.Id where ocnt.ctype = 1 and op.Id = $id";
		$pro = $GLOBALS['db']->getOne($sql);
		if(!$pro){
			redirect("index-index|true");//产品不存在
		}

		/** 图片内容 **/
		$pro['pics'] = $GLOBALS['db']->getAll("select * from ".tn('album')." where alb_type =1 and fk_id = $id order by cover desc");

		/** 属性处理 **/
		$attr = unserialize($pro['attr']);
		for ($i=0;$i<sizeof($attr['val']);$i++){
			if(preg_match('/^(http:\/\/)(.*?)\.(.*)+$/',$attr['val'][$i])){
				$attr['val'][$i] = "<a href='{$attr['val'][$i]}' target='_blank'>{$attr['val'][$i]}</a>";
			}
		}
		$pro['attr'] = $attr;
		if($pro['pro_tags']){
			$pro['pro_tags'] = explode(",",$pro['pro_tags']);
		}
		if($pro['pro_keywords']){
			$pro['pro_keywords'] = explode(",",$pro['pro_keywords']);
		}
		$GLOBALS['tpl']->assign('pro',$pro);
		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('pro-index'),'text'=>'产品中心');
		$q = $GLOBALS['db']->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$pro['path']).")");
		while ($row=$GLOBALS['db']->fetch_array($q)) {
			$local[] = array('url'=>parse_uri('pro-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
		}
		$local[] = array('url'=>parse_uri('pro-cat-id-%d',$pro['cat_id']),'text'=>$pro['cat_name']);
		$local[] = array('url'=>parse_uri('pro-v-id-%d',$pro['Id']),'text'=>$pro['pro_name']);
		$GLOBALS['tpl']->assign('local',$local);
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));
	}
	/** 浏览计数 **/
	if(!isset($_COOKIE["views"]["pro-$id"])){
		$GLOBALS['db']->execute("update ".tn('product')." set pro_views = pro_views+1 where id = $id");
		setcookie("views[pro-$id]", "ora",time()+3600);
	}
	$GLOBALS['tpl']->assign('views',$GLOBALS['db']->count("select pro_views from ".tn('product')." where Id = $id"));

	$GLOBALS['tpl']->display("pro.html");
}

/** 产品首页 **/
else if($act == 'index'){
	if(!$GLOBALS['tpl']->isCached("pro-index")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 产品信息 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
		$GLOBALS['tpl']->assign('cates',$cates);
		/*分类下属内容*/
		$catPros = array();
		for ($j=0;$j<sizeof($cates);$j++){
			$tmp = $cates[$j];
			unset($tmp['sub']);
			$idStr = cat_lib::getCatIdStr($tmp['Id']);
			$sql = "select op.Id,op.cat_id,op.pro_name,op.pro_keywords,op.pro_pic,oc.cat_name from ".tn('product')." op left join ";
			$sql .=" ".tn('cat')." oc on op.cat_id = oc.Id where oc.cat_type = 1 and op.pro_status = 1 and op.cat_id in ($idStr) order by op.Id desc limit 10";
			$tmp['pros'] = $GLOBALS['db']->getAll($sql);
			$catPros[] = $tmp;
		}
		$GLOBALS['tpl']->assign('catPros',$catPros);
		unset($cates);
		unset($catPros);
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));
		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('pro-index'),'text'=>'产品中心');
		$GLOBALS['tpl']->assign('local',$local);
	}
	$GLOBALS['tpl']->display("pro_index.html");
}
/** 产品分类 **/
else if($act == 'cat'){
	$id = intval($GLOBALS['_param']['id']);
	$pid= intval($GLOBALS['_param']['p']);
	$pid= $pid>0?$pid:1;
	import("lib.page");
	if(!$GLOBALS['tpl']->isCached("pro-cat-id-$id-p-$pid")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 产品信息 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
		$GLOBALS['tpl']->assign('cates',$cates);
		$curr_cat = $GLOBALS['db']->getOne("select * from ".tn('cat')." where id = $id");
		$GLOBALS['tpl']->assign('curr_cat',$curr_cat);
		if(!$curr_cat){
			redirect(HTTP_HOST,false);
		}
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));

		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>parse_uri('pro-index'),'text'=>'产品中心');
		$q = $GLOBALS['db']->query("select Id,cat_name from ".tn('cat')." where id in(".str_replace('-',',',$curr_cat['path']).")");
		while ($row=$GLOBALS['db']->fetch_array($q)) {
			$local[] = array('url'=>parse_uri('pro-cat-id-%d',$row['Id']),'text'=>$row['cat_name']);
		}
		$local[] = array('url'=>parse_uri('pro-cat-id-%d',$curr_cat['Id']),'text'=>$curr_cat['cat_name']);
		$GLOBALS['tpl']->assign('local',$local);

		/** 分页开始 **/
		$psize = $GLOBALS['_param']['psize']?$GLOBALS['_param']['psize']:$GLOBALS['ora_cfg']['page_size'];
		$idStr = cat_lib::getCatIdStr($id);
		$total = $GLOBALS['db']->count("select count(*) from ".tn('product')." where pro_status = 1 and cat_id in ($idStr) ");
		$pobj = new page('p',$pid,$total,$psize);
		$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
		$sql .= " where cat_id in (".$idStr.") and pro_status = 1 order by Id desc limit ".$pobj->sqlParam.") ";
		$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
		if($pid >= intval($pobj->maxPage/2)){
			$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
			$sql .= " where cat_id in (".$idStr.") and pro_status = 1 order by Id desc limit ".$pobj->sqlParam.") ";
			$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
		}
		$GLOBALS['tpl']->assign("pobj",$pobj);
		$url = get_urlStr();
		$GLOBALS['tpl']->assign('url',preg_replace('/\-?p(\-|_|=)\d+/i',"",$url));
		$GLOBALS['tpl']->assign('pros',$GLOBALS['db']->getAll($sql));
	}
	$GLOBALS['tpl']->display("pro_cat.html");

}

?>